package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.HisUser;
import com.his.entity.Patient;
import com.his.mapper.PatientMapper;
import com.his.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Patient)表服务实现类
 *
 * @author makejava
 * @since 2020-10-23 18:14:20
 */
@Service("patientService")
public class PatientServiceImpl implements PatientService {
    @Resource
    private PatientMapper patientMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Patient queryById(Integer id) {
        return this.patientMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param patient 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Patient> queryAll(Patient patient) {
        return this.patientMapper.queryAll(patient);
    }

    /**
     * 新增数据
     *
     * @param patient 实例对象
     * @return 实例对象
     */
    @Override
    public Rsp insert(Patient patient) {
        Patient p = patientMapper.getByTellphone(patient.getTellphone());
        if(p!=null){
            return Rsp.fail().msg("手机号码重复");
        }
        this.patientMapper.insert(patient);
        return Rsp.success(null).msg("注册成功");
    }

    /**
     * 修改数据
     *
     * @param patient 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(Patient patient) {
        return this.patientMapper.update(patient)>0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.patientMapper.deleteById(id) > 0;
    }


    @Override
    public Patient login(Patient patient) {
        return patientMapper.getByTellphoneAndPassword(patient);
    }

    @Override
    public PageInfo<Patient> searchSomePatientPage(PageParam<Patient> pageParam, HisUser user) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        Patient model = pageParam.getModel();
        if(user.getRoles().contains("doctor")){
            model.setRecordId(user.getId());
        }
        return new PageInfo<>(patientMapper.queryAllByModel(model));
    }

    /**
     * 获取所有病人列表
     * @return
     */
    @Override
    public Rsp selectPatientList() {
        return Rsp.success(patientMapper.selectPatientList());
    }
}
